$(function(){
var dlg = $('#login_dialog');
dlg.dialog(
{
    modal: true, title: "Вход",
    height: 280, width: 250,
    autoOpen: false, resizable: false,
	draggable: false, closeOnEscape: false,
    open: function () { 
		$('div[aria-describedby="login_dialog"]').shadow();
		$('div[aria-describedby="login_dialog"] button.ui-dialog-titlebar-close').hide();
	},
    close: function () {
        $(this).dialog("destroy");
    }
});
dlg.dialog('open');

$('#login_form input[name="submit"]').button().click( function() {
    var reqData = {
		q: 'login/check',
        uname: $('form #uname').val(),
        passw: $('form #passw').val()
    };            
	$('#login_mess').hide();
    $.post('index.php', reqData, reqResp, 'json' );
	return false;
});

$('#login_form input[name="cansel"]').button().click( function() {
	dlg.dialog("destroy");
	dlg.empty();
});

function reqResp(rsp, textStatus) {
    if (rsp.errcode) {
        $('#login_mess').html(rsp.errmess)
            .removeClass("ui-state-default")
            .addClass("ui-state-error")
            .fadeIn('slow', function() {
                setTimeout( function() { $('#login_mess').fadeOut('normal')}, 5000 );
            });                
        return;
    }
    else {
		if( typeof app == 'undefined' ) {
			document.location.href = rsp.data.url;
		}
		else {
			app.customer.full_name = rsp.data.full_name;
			app.customer.cat_id = rsp.data.cat_id;
			app.customer.priv = rsp.data.priv;
			app.setMenuPerm();
			dlg.dialog('destroy');
			dlg.empty();
			document.title = app.customer.full_name;
		}
    }
}
}); // End of onready
